<?php
/**
 * File: WeixinController.php.
 * User: Yan<me@xiaoyan.me>
 * DateTime: 2017-12-19 22:13
 */

namespace App\Controller;


use App\Core\Request;
use App\Core\WebController;
use App\Models\UserInfo;

class WeixinController extends WebController
{
    const GET_TYPE = 'detail';
    const GET_OPENID = 'http://www.xkl-bj.com/ol/test/?act=get_openid&user_info=' . self::GET_TYPE . '&callback=';

    public function auth(Request $request)
    {
        if ($request->get('open_id')) {
            $_SESSION[WX_USER_KEY] = $request->get('open_id');
            if(self::GET_TYPE == 'detail'){
                $_SESSION[WX_USER_KEY . '_user'] = [
                    'nick_name' => urldecode($request->get('nickname')),
                    'headimgurl' => urldecode($request->get('headimgurl')),
                ];
            }
            jump($_SESSION['auth_callback']);
        }
        $callback = urldecode($request->get('callback'));
        $_SESSION['auth_callback'] = $callback;
//        print_r($_SESSION);
        jump(self::GET_OPENID . urlencode(getFullUrl('/weixin/auth')));
    }

    public function bind(Request $request)
    {
        $open_id = $_SESSION[WX_USER_KEY];
        $avatar = '/res/assets/upload/default_avatar.png';
        if ($request->get('action') == 'register') {
            $user = new UserInfo();
            if (!$user->has(['user_open_id' => $open_id])) {
                $username = $request->get('user_name');
                if (!$username) {
                    $username = '微信用户' . substr($open_id, 5);
                } else $username = urldecode($username);
                if ($request->get('head_img')) {
                    $avatar = urldecode($request->get('head_img'));
                }

                $user->insert([
                    'user_name' => $username,
                    'user_open_id' => $open_id,
                    'user_avatar' => $avatar,
                    'user_role' => 'default'
                ]);
            }
            jump('/');
        }
        if ($request->get('action') == 'bind') {
            $condition = [];
            $user = new UserInfo();
            if (!$request->post('idno') && !($request->post('student_no'))) {
                ajaxError('必须填写学号或者身份证中的一项数据');
            }
            if (!($userData = $user->findObjectByStudentOrIdno($request->post('student_no'), $request->post('idno')))) {
                ajaxError('要绑定的数据不存在');
            }
            if ($userData->user_open_id == $request->openid) {
                ajaxSuccess();
            }
            if ($userData->user_open_id && $userData->user_open_id != $request->openid) {
                ajaxError('要绑定的用户已被其他微信账号绑定');
            }
            $user->update(['user_open_id' => $request->openid, 'user_role' => 'normal'], ['uid' => $userData->uid]);
            ajaxSuccess();
        }
        if ($request->get('action') == 'done' && isset($_SESSION['bind_callback'])) {
            if (!$_SESSION['bind_callback']) {
                jump('/');
            }
            jump(urldecode($_SESSION['bind_callback']));
        }

        $callback = urldecode($request->get('callback'));
        $_SESSION['bind_callback'] = $callback;
        if(self::GET_TYPE == 'detail') {
            $this->assign('wx_user_info', $_SESSION[WX_USER_KEY . '_user']);
        }else{
            $this->assign('wx_user_info',[
                'nick_name'=>'','headimgurl'=>$avatar
            ]);
        }
        $this->render('bind', ['page_title' => '绑定用户']);
    }
}